//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.TUserSys;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.domain.vo.TUserSysVo;
import com.ruoyi.system.mapper.TUserSysMapper;
import com.ruoyi.system.service.ITUserSysService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class TUserSysServiceImpl implements ITUserSysService {
    @Autowired
    private TUserSysMapper tUserSysMapper;

    public TUserSysServiceImpl() {
    }

    public TUserSysVo selectTUserSysById(String id) {
        TUserSysVo tUserSysVo = new TUserSysVo();
        tUserSysVo.setCfgId(id);
        List<TUserSysVo> tUserSysVoList = this.tUserSysMapper.selectTUserSysList(tUserSysVo);
        return Optional.ofNullable(tUserSysVoList).isPresent() ? (TUserSysVo)tUserSysVoList.get(0) : null;
    }

    public List<TUserSysVo> selectTUserSysList(TUserSysVo tUserSysVo) {
        return this.tUserSysMapper.selectTUserSysList(tUserSysVo);
    }

    @Transactional
    public int insertTUserSys(List<String> sysIds) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String userName = loginUser.getUsername();
        if (ObjectUtils.isEmpty(userName)) {
            throw new ServiceException("用户登入信息不能为空");
        } else {
            List<String> sysIdList = this.tUserSysMapper.selectSysIdByUserName(userName);

            for(int i = 0; i < sysIds.size(); ++i) {
                String sysId = (String)sysIds.get(i);
                if (!sysIdList.contains(sysId)) {
                    TUserSys tUserSys = new TUserSys();
                    tUserSys.setCfgId(IdUtils.simpleUUID());
                    tUserSys.setSysId(sysId);
                    tUserSys.setUserName(userName);
                    tUserSys.setRankBy(sysIdList.size() + i + 1);
                    this.tUserSysMapper.insertTUserSys(tUserSys);
                }
            }

            return 1;
        }
    }

    public int updateTUserSys(TUserSys tUserSys) {
        return this.tUserSysMapper.updateTUserSys(tUserSys);
    }

    public int deleteTUserSysByIds(String[] ids) {
        return this.tUserSysMapper.deleteTUserSysByIds(ids);
    }

    public int deleteTUserSysById(String id) {
        return this.tUserSysMapper.deleteTUserSysById(id);
    }

    public List<TUserSysVo> selectIntegrationList() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return this.tUserSysMapper.selectIntegrationList(loginUser.getUsername());
    }

    public void updateRankByCfgId(String cfgId, Integer rank) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        TUserSysVo tUserSysVo = new TUserSysVo();
        tUserSysVo.setUserName(loginUser.getUsername());
        List<TUserSysVo> list = this.tUserSysMapper.selectTUserSysList(tUserSysVo);
        TUserSys tUserSys = this.tUserSysMapper.selectTUserSysById(cfgId);
        int i;
        if (tUserSys.getRankBy() > rank) {
            for(i = 0; i < list.size(); ++i) {
                if (i < tUserSys.getRankBy() && i + 2 > rank) {
                    this.tUserSysMapper.updateRankByCfgId(((TUserSysVo)list.get(i)).getCfgId(), ((TUserSysVo)list.get(i)).getRankBy() + 1);
                }
            }
        }

        if (tUserSys.getRankBy() < rank) {
            for(i = 0; i < list.size(); ++i) {
                if (i + 2 > tUserSys.getRankBy() && i < rank) {
                    this.tUserSysMapper.updateRankByCfgId(((TUserSysVo)list.get(i)).getCfgId(), ((TUserSysVo)list.get(i)).getRankBy() - 1);
                }
            }
        }

        this.tUserSysMapper.updateRankByCfgId(cfgId, rank);
    }
}
